/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : FindBeginningEndingLetter.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Dec 25, 2008       Raghu Sarraff			       Created
 */
package lwf.handler.english.word;

import java.util.ArrayList;
import java.util.List;

import net.lwf.entity.AttemptedQuestion;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.util.ImageHtml;
import net.lwf.handler.util.WordUtil;
import net.lwf.number.util.RandomNumberGenerator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.helper.DisplayFormatter;
import lwf.handler.helper.FormatAttribute;

/**
 * Type the class description here
 *  
 */
public class FindBeginningEndingLetter extends ProcessHandler implements
		IProcessHandler {
	
	private String questionDesc = null;
	private String question = "What is the beginning letter sound of the image word above?";
	String imageword = null;
	private FormatAttribute format;
	
	public FindBeginningEndingLetter() {
		init();
	}
	
	/**
	 * @see net.lwf.handler.IProcessHandler#getDisplayFormatter()
	 */
	public DisplayFormatter getDisplayFormatter() {
		format = new FormatAttribute();
		
		format.setQuestionNumber(getQuestionNum());
		format.setQuestionTable(this.generateQuestionTable());
		format.setQuestionType(3); 
		
		// Generate answer choices
		List lstData = new ArrayList();
		for(int i=0; i<imageword.length(); i++) {
			lstData.add(imageword.substring(i, i+1));
		}
		format.setRadioAnswers(lstData);

		
		return new DisplayFormatter(format);
	}
	

	public String getQuestionDesc() {
		return this.questionDesc;
	}
	
	private void init() {

		this.imageword = WordUtil.getAImageWord();
		int findletter = RandomNumberGenerator.generate(1,2);
		String findword = WordUtil.getBeginningLetter(imageword);
		this.questionDesc = "What is the beginning letter sound for <b>" + imageword + "</b>?";
		if(findletter == 2) {
			findword = WordUtil.getEndingLetter(imageword);
			question = "What is the ending letter sound of the image word above?";
			this.questionDesc = "What is the ending letter sound for <b>" + imageword + "</b>?";
		}
		setAnswer(findword);
		
		// Generate answer choices
//		List lstData = new ArrayList();
//		for(int i=0; i<imageword.length(); i++) {
//			lstData.add(imageword.substring(i, i+1));
//		}
//		setRadioAnswers(lstData);
		
	} //init
	
	private String generateQuestionTable() {
		StringBuffer sbQBlock = new StringBuffer();
		String imagehtml = null;
		sbQBlock.append("<table width=\"100%\" border=\"0\" align=\"center\" cellspacing=\"0\" cellpadding=\"0\">");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td>");
				    sbQBlock.append("<table>");
				    	sbQBlock.append("<tr>");
				    		sbQBlock.append("<td class=\"questionmedium\">");
				    			sbQBlock.append("What is this picture?");		
				    		sbQBlock.append("</td>");
				    		sbQBlock.append("<td align=\"center\">");
				    			sbQBlock.append(ImageHtml.getWordImage(this.imageword));		
			    			sbQBlock.append("</td>");
				    	sbQBlock.append("</tr>");
				    sbQBlock.append("</table>");
				sbQBlock.append("</td>");
			sbQBlock.append("</tr>");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td>&nbsp;</td>");
		    sbQBlock.append("</tr>");
		    sbQBlock.append("<tr>");
		    	sbQBlock.append("<td class=\"questionmedium\">");
		    		sbQBlock.append(question);
		    		sbQBlock.append("</td>");
			sbQBlock.append("</tr>");
		sbQBlock.append("</table>");
		
		return sbQBlock.toString();
		
	}

	//------------------------------------------------ Methods for PDF generation on the browser
	public void generate(Document document) {
	
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return null;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	public boolean matchAnswer(String useranswer, AttemptedQuestion aq) {
		return false;
	}

	@Override
	public String getPdfNote() {
		return null;
	}

}

